package com.kipling.kio.server.intf;

import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;

import com.kipling.kio.core.GroupContext;
import com.kipling.kio.core.exception.KioDecodeException;
import com.kipling.kio.core.intf.KioHandler;
import com.kipling.kio.core.intf.Packet;
import com.kipling.kio.json.Json;
import com.kipling.kio.server.ServerGroupContext;

/** 
 * @author KiplingLee E-mail: kiplinglee@163.com
 * @version 创建时间：2017年5月2日 下午5:01:04 
 * 
 */
public abstract class ServerKioHandler <P extends Packet, R> implements KioHandler<P, R> {
	
	public static int bufferSize = 2048;
	
	ServerGroupContext<P, R> serverGroupContext=  null;
	@Override
	public void handlerSelectionKey(SocketChannel socketChannel,ByteBuffer byteBuffer,ServerGroupContext< P, R> serverGroupContext) throws Exception {
		this.serverGroupContext=serverGroupContext;
//            String received = Charset.forName("utf-8").newDecoder().decode(byteBuffer).toString();
//            System.out.println("received+++++:"+received);
//            DecodeRunnable<P, R> decodeRunnable = serverGroupContext.getDecodeRunnable();
//            byteBuffer.flip();
//            decodeRunnable.setNewByteBuffer(byteBuffer);
//			decodeRunnable.run();
			String received = Charset.forName("utf-8").newDecoder().decode(byteBuffer).toString();
			System.out.println("run:received:"+received);
			Packet packet = Json.toBean(received, Packet.class);
			handler(packet,serverGroupContext);
           // String received = Charset.forName("utf-8").newDecoder().decode(byteBuffer).toString();
            //System.out.println(received);
       
		
	}
	
	
	
	@Override
	public R handler(Packet packet, ServerGroupContext<P, R> serverGroupContext)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ByteBuffer encode(P packet,
			GroupContext< P, R> groupContext) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public P decode(ByteBuffer buffer) throws KioDecodeException {
		// TODO Auto-generated method stub
		return null;
	}

}
